package online.inote.naruto.api.access.admin.modules.system.controller;

import online.inote.naruto.api.access.admin.modules.base.controller.BaseController;
import online.inote.naruto.api.access.admin.modules.system.entity.SystemEntity;
import online.inote.naruto.api.access.admin.modules.system.service.SystemService;
import online.inote.naruto.common.utils.response.Code;
import online.inote.naruto.common.utils.response.ExtendResponse;
import online.inote.naruto.common.utils.response.Response;
import online.inote.naruto.utils.Page;
import online.inote.naruto.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description 系统Controller
 * @author XQF.Sui
 * @date 2021/07/31 11:55
 */
@RestController
@RequestMapping(value = "system")
public class SystemController extends BaseController {

  private final Logger logger = LoggerFactory.getLogger(getClass());

  private final SystemService systemService;

  public SystemController(SystemService systemService) {
    this.systemService = systemService;
  }

  /**
   * 分页查询
   *
   * @param page 分页信息
   * @return 服务分页数据
   */
  @PostMapping(value = "getPage")
  public Response<Page<SystemEntity>> getPage(@RequestBody Page<SystemEntity> page) {
    systemService.getPage(page);
    return ExtendResponse.success(page);
  }

  /**
   * 保存
   *
   * @param system 系统信息
   * @param result 验证结果
   * @return 保存结果信息
   */
  @PostMapping(value = "save")
  public Response<String> save(@RequestBody @Valid SystemEntity system, BindingResult result) {
    if (result.hasErrors()) {
      return ExtendResponse.fail(
          StringUtils.join(
              result.getAllErrors().stream()
                  .map(ObjectError::getDefaultMessage)
                  .collect(Collectors.toList())));
    }

    try {
      return systemService.save(system, true);
    } catch (Exception e) {
      logger.error("保存失败", e);
      return ExtendResponse.fail("保存失败");
    }
  }

  /**
   * 启用/禁用
   *
   * @param system 系统信息
   * @return 启用结果信息
   */
  @PostMapping(value = "enable")
  public Response<SystemEntity> enable(@RequestBody SystemEntity system) {

    if (StringUtils.isBlank(system.getId()) || Objects.isNull(system.getEnable())) {
      return ExtendResponse.create(Code.PARAM_REQUIRED);
    }

    SystemEntity existsSystemInfo = systemService.details(system.getId());

    if (Objects.isNull(existsSystemInfo)) {
      return ExtendResponse.fail("未查询到对于的系统信息");
    }

    existsSystemInfo.setEnable(system.getEnable());
    systemService.save(existsSystemInfo, false);

    return ExtendResponse.success();
  }

  /**
   * 详情
   *
   * @param id 系统ID
   * @return 系统详情
   */
  @GetMapping(value = "details/{id}")
  public Response<SystemEntity> details(@PathVariable String id) {
    return ExtendResponse.success(systemService.details(id));
  }
}
